package com.star.langchain.learn.controller;

import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

import com.star.langchain.learn.service.ToolConfirmationService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 工具调用确认控制器
 * 提供用户确认/拒绝工具调用的API接口
 *
 * @author star
 */
@Slf4j
@RestController
@RequestMapping("/api/tool-confirmation")
public class ToolConfirmationController {

    @Autowired
    private ToolConfirmationService toolConfirmationService;

    /**
     * 获取待确认的工具调用信息
     *
     * @param confirmationId 确认ID
     * @return 工具调用信息
     */
    @GetMapping("/{confirmationId}")
    public ResponseEntity<Map<String, Object>> getConfirmationInfo(@PathVariable String confirmationId) {
        ToolConfirmationService.ToolConfirmationInfo info = 
                toolConfirmationService.getConfirmationInfo(confirmationId);
        
        if (info != null) {
            Map<String, Object> response = new HashMap<>();
            response.put("confirmationId", info.getConfirmationId());
            response.put("toolName", info.getToolName());
            response.put("toolArguments", info.getToolArguments());
            response.put("requestTime", info.getRequestTime());
            response.put("timeoutSeconds", info.getTimeoutSeconds());
            return ResponseEntity.ok(response);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 用户确认工具调用
     *
     * @param confirmationId 确认ID
     * @return 操作结果
     */
    @PostMapping("/{confirmationId}/confirm")
    public ResponseEntity<Map<String, Object>> confirm(@PathVariable String confirmationId) {
        boolean success = toolConfirmationService.confirm(confirmationId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "工具调用已确认" : "未找到待确认的工具调用");
        return ResponseEntity.ok(response);
    }

    /**
     * 用户拒绝工具调用
     *
     * @param confirmationId 确认ID
     * @return 操作结果
     */
    @PostMapping("/{confirmationId}/reject")
    public ResponseEntity<Map<String, Object>> reject(@PathVariable String confirmationId) {
        boolean success = toolConfirmationService.reject(confirmationId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "工具调用已拒绝" : "未找到待确认的工具调用");
        return ResponseEntity.ok(response);
    }
}

